package com.hcr.kingdeeutil;

import com.google.gson.*;
import com.hcr.kingdeeutil.common.Basic;
import com.hcr.kingdeeutil.common.Parameters;
import com.hcr.kingdeeutil.query.QueryParameters;
import com.hcr.kingdeeutil.result.kingdeeResult;
import com.hcr.kingdeeutil.util.InvokeHelper;
import org.apache.commons.lang3.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author songwei.zhang
 * @version 0.1.0
 * @Description
 * @create 2021-03-24 11:08
 **/
public class KingdeeUtil {
    private static Basic basic;
    private static Gson gson;
    private static Parameters parameters;

    static {
        basic = new Basic();
        basic.setDeleteEntry(true);
        basic.setEntryBatchFill(true);
        basic.setValidateFlag(true);
        basic.setNumberSearch(true);
        parameters = new Parameters();
        gson = new GsonBuilder()
                .setDateFormat("yyyy-MM-dd'T'HH:mm:ss")
                .create();
    }

    /**
     * 登录
     *
     * @param dbId
     * @param uid
     * @param pwd
     * @param lang
     * @return
     * @throws Exception
     */
    public static String login(String POST_K3CloudURL, String dbId, String uid, String pwd, int lang) throws Exception {
        return InvokeHelper.Login(POST_K3CloudURL, dbId, uid, pwd, lang);
    }

    /**
     * 保存
     *
     * @param sFormId
     * @param basicObj
     * @param object
     * @throws Exception
     */
    public static kingdeeResult save(String sFormId, Basic basicObj, Object object, String accessToken) throws Exception {
        if (null == basicObj) {
            basicObj = basic;
        }
        if (null != object) {
            basicObj.setModel(object);
        }
        System.out.println(gson.toJson(basicObj));
        return InvokeHelper.Save(sFormId, gson.toJson(basicObj), accessToken);
    }

    /**
     * 保存
     *
     * @param sFormId
     * @param object
     * @return
     * @throws Exception
     */
    public static kingdeeResult save(String sFormId, Object object, String accessToken) throws Exception {
        if (null != object) {
            basic.setModel(object);
        }
        System.out.println(gson.toJson(basic));
        return InvokeHelper.Save(sFormId, gson.toJson(basic), accessToken);
    }

    /**
     * 提交
     *
     * @param sFormId
     * @param parameter
     * @throws Exception
     */
    public static kingdeeResult submit(String sFormId, Parameters parameter, String accessToken) throws Exception {
        if (null == parameter) {
            parameter = parameters;
        }
        return InvokeHelper.Submit(sFormId, gson.toJson(parameter), accessToken);
    }

    /**
     * 审核
     *
     * @param sFormId
     * @param parameter
     * @throws Exception
     */
    public static kingdeeResult audit(String sFormId, Parameters parameter, String accessToken) throws Exception {
        if (null == parameter) {
            parameter = parameters;
        }
        return InvokeHelper.Audit(sFormId, gson.toJson(parameter), accessToken);
    }

    /**
     * 查看
     *
     * @param sFormId
     * @param parameter
     * @throws Exception
     */
    public static kingdeeResult view(String sFormId, Parameters parameter, String accessToken) throws Exception {
        if (null == parameter) {
            parameter = parameters;
        }
        return InvokeHelper.View(sFormId, gson.toJson(parameter), accessToken);
    }

    /**
     * 查询单据详情
     *
     * @param parameter
     * @return
     * @throws Exception
     */
    public static List<Object> executeBillQuery(QueryParameters parameter, String accessToken) throws Exception {
        List<Object> queryList = new ArrayList<>();
        if (null != parameter || StringUtils.isNotEmpty(parameter.getFieldKeys())) {
            String result = InvokeHelper.executeBillQuery(gson.toJson(parameter), accessToken);
            // 拆分查询入参字段
            String[] fieldKeys = parameter.getFieldKeys().split(",");
            JsonElement tradeElement = JsonParser.parseString(result);
            JsonArray jsonArray = tradeElement.getAsJsonArray();
            JsonArray sonJsonArray;
            Map<String, Object> resultObj = null;
            for (JsonElement jsonElement : jsonArray) {
                resultObj = new HashMap<>();
                sonJsonArray = jsonElement.getAsJsonArray();
                for (int i = 0; i < sonJsonArray.size(); i++) {
                    Object son = sonJsonArray.get(i);
                    resultObj.put(fieldKeys[i], son);
                }
                queryList.add(resultObj);
            }
        }
        return queryList;
    }

    /**
     * 删除
     *
     * @param sFormId
     * @param parameter
     * @throws Exception
     */
    public static kingdeeResult delete(String sFormId, Parameters parameter, String accessToken) throws Exception {
        if (null == parameter) {
            parameter = parameters;
        }
        return InvokeHelper.Delete(sFormId, gson.toJson(parameter), accessToken);
    }
}
